package es.ats.codington.festival.daos;

import java.util.ArrayList;
import java.util.Properties;

import org.springframework.jdbc.core.JdbcTemplate;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.interfaces.daos.IEventDAOImp;
import es.ats.codington.festival.mappers.EventMapper;

public class EventDAOImp implements IEventDAOImp{

	int nRows;
	private JdbcTemplate jdbcTemplateObject;
	private Properties queryProperties;
	
	public EventDAOImp(JdbcTemplate jdbcTemplateObject,Properties queryProperties){
		this.jdbcTemplateObject = jdbcTemplateObject;
	    this.queryProperties = queryProperties;
	}
	
	public EventDAOImp(){
		
	}

	public Event getEventById(int eventId, String usTickets){
		Event event = new Event();
		event = (Event) jdbcTemplateObject.queryForObject(queryProperties.getProperty("geteventbyid"), new Object[]{eventId}, new EventMapper());
		event.setUsTickets(usTickets);
		
		return event;
	}
	
	public Event getEventById(int eventId){
		Event event = new Event();
		event = (Event) jdbcTemplateObject.queryForObject(queryProperties.getProperty("geteventbyid"), new Object[]{eventId}, new EventMapper());
		
		return event;
	}
	
	public ArrayList<Event> getEventList(){
		ArrayList<Event> listEvents = new ArrayList<Event>();
		listEvents = (ArrayList<Event>) jdbcTemplateObject.query(queryProperties.getProperty("geteventlist"), new EventMapper());
				
		return listEvents;
	}
	
	public int insertEvent(Event event){
		nRows=0;
		nRows = jdbcTemplateObject.update(queryProperties.getProperty("insertevent"), event.getEventName(), event.getEventDescription(), event.getDuration(), event.getEventType(), event.getTickets(), event.getPlaces().getPlaceId());

		return nRows;
	}
	
	public int deleteEvent(int eventId){
		nRows=0;
		nRows = jdbcTemplateObject.update(queryProperties.getProperty("deleteevent"),eventId);

		return nRows;
	}
	
	public int updateEvent(Event uEvent){
		nRows=0;
		nRows = jdbcTemplateObject.update(queryProperties.getProperty("updateevent"), uEvent.getEventName(), uEvent.getEventDescription(), uEvent.getDuration(), uEvent.getEventType(), uEvent.getTickets(), uEvent.getPlaces().getPlaceId(), uEvent.getEventId());

		return nRows;
	}

	
}